package com.song.base.dao;

import com.song.base.bean.PageResult;

import java.util.List;
import java.util.Map;

public interface BaseMapper<T> {


    int deleteByPrimaryKey(Integer id);

    int insert(T record);

    int insertSelective(T record);

    T selectByPrimaryKey(Integer id);

    int updateByPrimaryKey(T record);

    int updateByPrimaryKeySelective(T record);

    Integer count(Map<String, Object> map);

    List<T> findByMap(Map<String, Object> map);

    /**
     * 分页查询
     *
     * @param map pageNo 页码,pageSize 每页条数
     * @return
     */
    default PageResult<T> pageFind(Map<String, Object> map) {
        if (map != null) {
            Object pageNo = map.remove("pageNo");
            Object pageSize = map.remove("pageSize");
            if (pageNo != null && pageSize != null) {
                map.put("pageOffset", (Integer.parseInt(pageNo.toString()) - 1) * Integer.parseInt(pageSize.toString()));
                map.put("pageLimit", pageSize);
            }
        }
        Integer count = count(map);
        PageResult<T> result = new PageResult<T>(count);
        if(count > 0){
            List<T> list = findByMap(map);
            result.setData(list);
        }
        return result;
    }

}
